#include <game/player.h>

#include <physics/pworld.h>

#include <QDomDocument>

Player::Player(PWorld *world, PVector startPos, double startAngle, QString name, QObject *parent, bool createShip) :
    QObject(parent)
{
    myWorld = world;
    myName = name;
    myStartPos = startPos;
    myStartAngle = startAngle;

    if (createShip) {
        PShip *ship = new PShip(myWorld, name);
        ship->setPos(startPos);
        ship->setAlpha(myStartAngle);
        myWorld->addObject(ship);
    }

    myColor = QColor(255, 0, 0);
    alive = true;
    myLives = MAX_LIVES;
}

void Player::pressAccelerate() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setAccelerate(true);
}

void Player::pressFire() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setFire(true);
}

void Player::pressLeft() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setLeft(true);
}

void Player::pressRight() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setRight(true);
}

void Player::releaseAccelerate() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setAccelerate(false);
}

void Player::releaseFire() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setFire(false);
}

void Player::releaseLeft() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setLeft(false);
}

void Player::releaseRight() {
    PShip *ship = myWorld->getShip(myName);
    if (!alive || ship == 0) {
        return;
    }
    ship->setRight(false);
}

void Player::setColor(QColor color) {
    myColor = color;
}

QColor Player::getColor() const {
    return myColor;
}

PShip *Player::getShip() {
    if (!myWorld->getShip(myName)) {
        shipDied();
    }
    if (alive) {
        return myWorld->getShip(myName);
    }

    return 0;
}

void Player::shipDied() {
    alive = false;
    if (myLives > 1) {
        myLives--;
        PShip *ship = new PShip(myWorld, myName);
        ship->setPos(myStartPos);
        ship->setAlpha(myStartAngle);
        myWorld->addObject(ship);

        alive = true;
    }
}

bool Player::isAlive() const {
    return alive;
}

int Player::getLives() const {
    return myLives;
}

QString Player::getName() const {
    return myName;
}

void Player::serialize(QDomDocument *doc, QDomElement *elem) const {
    qDebug("serialize player");

    QDomElement name = doc->createElement("Name");
    QDomText name_v = doc->createTextNode(myName);
    name.appendChild(name_v);
    elem->appendChild(name);

    QDomElement color = doc->createElement("Color");
    QDomText color_v = doc->createTextNode(myColor.name());
    color.appendChild(color_v);
    elem->appendChild(color);

    QDomElement startAngle = doc->createElement("StartAngle");
    QDomText startAngle_v = doc->createTextNode(QString("%1").arg(myStartAngle));
    startAngle.appendChild(startAngle_v);
    elem->appendChild(startAngle);

    QDomElement lives = doc->createElement("Lives");
    QDomText lives_v = doc->createTextNode(QString("%1").arg(myLives));
    lives.appendChild(lives_v);
    elem->appendChild(lives);

    QDomElement startPos = doc->createElement("StartPos");
    myStartPos.serialize(doc, &startPos);
    elem->appendChild(startPos);
}

Player *Player::deserialize(QDomNode e, PWorld *world) {
    QString name = "";
    QColor color;
    double startAngle = 0;
    int lives = 0;
    PVector startPos;
    QDomNode n = e.firstChild();
    while (!n.isNull()) {
        QString tagname = n.toElement().tagName();
        if (tagname == "Name") {
            name = n.firstChild().toText().data();
        } else if (tagname == "Color") {
            color = QColor(n.firstChild().toText().data());
        } else if (tagname == "StartAngle") {
            startAngle = n.firstChild().toText().data().toDouble();
        } else if (tagname == "StartPos") {
            startPos = PVector::deserialize(n);
        } else if (tagname == "Lives") {
            lives = n.firstChild().toText().data().toInt();
        }
        n = n.nextSibling();
    }
    Player *p = new Player(world, startPos, startAngle, name, world, false);
    p->myColor = color;
    p->myLives = lives;
    return p;
}

PVector Player::getStartPos() const {
    return myStartPos;
}

double Player::getStartAngle() const {
    return myStartAngle;
}

QString Player::getType() const {
    return QString("Player");
}
